import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
    {
        path: '/404',
        name: '404',
        component: () => import('../views/404.vue')
    },
    /*{
        path: '/',
        component: () => import('../views/Manage.vue'),
        redirect: "/home",
        children: [
            { path: 'home', name: '首页', component: () => import('../views/Home.vue')},
            { path: 'user', name: '用户管理', component: () => import('../views/User.vue')},
            { path: 'person', name: '个人中心', component: () => import('../views/Person.vue')},
            { path: 'shop', name: '店铺信息', component: () => import('../views/shop/Shop')},
            { path: 'myShop', name: '我的店铺', component: () => import('../views/shop/MyShop')},
            { path: 'dish', name: '菜品信息', component: () => import('../views/dish/Dish.vue')},
            { path: 'mydish', name: '我的菜品', component: () => import('../views/dish/MyDish.vue')},
            { path: 'adminOrder', name: '订单信息', component: () => import('../views/oreders/AdminOrder')},
            { path: 'historyOrder', name: '历史订单', component: () => import('../views/oreders/HistoryOrder.vue')},
            { path: 'newOrder', name: '新增订单', component: () => import('../views/oreders/NewOrder.vue')},
            { path: 'role', name: '角色管理', component: () => import('../views/Role.vue')},
            { path: 'menu', name: '菜单管理', component: () => import('../views/Menu.vue')},
        ]
    },*/

    {
        path: '/about',
        name: 'About',
        component: () => import('../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
    router.matcher = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
}

export const setRouters = () => {
    const storeMenus = localStorage.getItem("menus");
    if (storeMenus){
        // 获取当前的路由对象名称数组
        const currentRouteNames = router.getRoutes().map(v => v.name)
        if (!currentRouteNames.includes('Manage')) {
            //拼接动态路由
            const manageRoute = {path:'/',name:'Manage',component:() => import('../views/Manage.vue'),redirect:"/home",children:[
                    { path: 'person', name: '个人信息', component: () => import('../views/Person.vue')},
                ]}
            const menus = JSON.parse(storeMenus)
            menus.forEach(item => {
                if (item.path){
                    let itemMenu = {path:item.path.replace("/",""),name:item.name,component:() => import('../views/'+ item.pagePath +'.vue')}
                    manageRoute.children.push(itemMenu)
                }else if(item.children.length) {
                    item.children.forEach(item => {
                        if (item.path){
                            let itemMenu = {path:item.path.replace("/",""),name:item.name,component:() => import('../views/'+ item.pagePath +'.vue')}
                            manageRoute.children.push(itemMenu)
                        }
                    })
                }

            })
            //动态添加到现在的路由对象中去
            router.addRoute(manageRoute)
        }

    }
}

// 重置我就再set一次路由
setRouters()

// 路由守卫
router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称，为了在Header组件中去使用
    store.commit("setPath")  // 触发store的数据更新
    // 未找到路由的情况
    if (!to.matched.length) {
        const storeMenus = localStorage.getItem("menus")
        if (storeMenus) {
            next("/404")
        } else {
            // 跳回登录页面
            next("/login")
        }
    }
    // 其他的情况都放行
    next()

})

export default router
